/*
 * Copyright (C) 2010 Seapeak.Xu
 *
 * fostmq may be copied only under the terms of the GNU General
 * Public License V3, which may be found in the fostmq source kit.
 *
 * Created on: 2010-8-27
 * Author: Seapeak.Xu
 */

#include <stdlib.h>
#include <stdio.h>

#include "fost_msg.h"
#include "fost_buff.h"
#include "fost_logger.h"

int buff2header(header_t *header, const char *buff)
{
	if (NULL == header)
	{
		log_error("File:%s,Line:%d,Function:%s.the argument is null.",
				__FILE__, __LINE__, __FUNCTION__);
		return -1;
	}
	if (NULL == buff)
	{
		log_error(
				"File:%s,Line:%d,Function:%s.the task header buffer is null.",
				__FILE__, __LINE__, __FUNCTION__);
		return -1;
	}
	header->protocol = *buff;
	header->state = buff2int(buff + 1);
	header->bodylen = buff2long(buff + 1 + sizeof(int));
	return 0;
}

int header2buff(const header_t *header, char *buff)
{
	if (NULL == header)
	{
		log_error("File:%s,Line:%d,Function:%s.the argument is null.",
				__FILE__, __LINE__, __FUNCTION__);
		return -1;
	}
	if (NULL == buff)
	{
		log_error(
				"File:%s,Line:%d,Function:%s.the task header buffer is null.",
				__FILE__, __LINE__, __FUNCTION__);
		return -1;
	}

	*buff = header->protocol;
	int2buff(header->state, buff + 1);
	long2buff(header->bodylen, buff + 1 + sizeof(int));
	return 0;
}
